import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { Link } from 'react-router'
import { Table, Spin, Form, Row, Col, Input, Button, Select, TreeSelect, DatePicker, Checkbox, message, Dropdown, Menu, Icon } from 'antd'
import _ from 'lodash'
import moment from 'moment'

import Ajax from 'lib/Ajax'
import { addConfig } from 'actions/config'
import { getNewTreeData, findRowData, getNameByCode, confirmDelete, downloadFile } from 'lib/fun'

const FormItem = Form.Item;
const Option = Select.Option;
const SelectTreeNode = TreeSelect.TreeNode;
const { RangePicker } = DatePicker;

class Filter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      uploadList: [],
      downloadRFailLoading: false,
      downloadFailLoading: false,
      downloadSuccessLoading: false
    }
  }

  componentDidMount(){
    if(this.props.type == 'rFail'&&this.props.visible){
      this.loadUploadList(this.props.batchId);
    }
  }

  componentWillReceiveProps(nextProps){
    if(nextProps.visible&&nextProps.type == 'rFail'&&(this.state.uploadList.length <= 0||this.props.batchId != nextProps.batchId)){
      this.loadUploadList(nextProps.batchId);
    }else if(!nextProps.visible){
      this.props.form.resetFields();
    }
  }

  loadUploadList = (batchId)=>{
    var self = this;
    Ajax({
      url: '/projectRegister/listUploadRecord',
      data: {
        batchId
      },
      success: function(data){
        self.setState({
          uploadList: data
        });
      },
      error: function(error){
        message.error(error);
      }
    });
  }

  download = ()=>{
    const self = this;
    this.props.form.validateFields((err, values) => {
      self.setState({
        downloadRFailLoading: true
      });
      downloadFile('/projectRegister/downRegisterFail', Object.assign({
        batchId: this.props.batchId
      }, values), {end: ()=>{
        self.setState({
          downloadRFailLoading: false
        });
      }})
    });
  }

  downloadFail = ()=>{
    const { batchId, isSummary } = this.props;
    this.setState({
      downloadFailLoading: true
    });
    downloadFile('/projectRegister/downAuditFail', Object.assign({
      batchId: isSummary?undefined:batchId,
      summaryId: isSummary?batchId:undefined
    }), {end: ()=>{
      this.setState({
        downloadFailLoading: false
      });
    }})
  }

  downloadSuccess = ()=>{
    const { batchId, isSummary } = this.props;
    this.setState({
      downloadSuccessLoading: true
    });
    downloadFile('/projectRegister/downRegisterSuccess', Object.assign({
      batchId: isSummary?undefined:batchId,
      summaryId: isSummary?batchId:undefined
    }), {end: ()=>{
      this.setState({
        downloadSuccessLoading: false
      });
    }})
  }

  handleSearch = (e) => {
    var self = this;
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      self.props.handleFilter(values);
    });
  }

  handleReset = () => {
    this.props.form.resetFields();
  }

  render() {
    const { getFieldDecorator } = this.props.form;
    const { type, failDown, successDown } = this.props;
    console.log(this.props);
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
    };
    return (<div className="ant-advanced-search-form">
          <Row gutter={20}>
            {
              type == "rFail"?<Col span={6}>
                <FormItem {...formItemLayout} label="上传记录">
                  {getFieldDecorator("uploadId")(
                    <Select
                      size="default"
                      allowClear={true}
                    >
                      {
                        (this.state.uploadList||[]).map(function(obj){
                          return (<Option key={obj.uploadId+""} value={ obj.uploadId+"" }>{ obj.createTime }</Option>)
                        })
                      }
                    </Select>
                  )}
                </FormItem>
              </Col>:""
            }
            <Col span={6}>
              <FormItem {...formItemLayout} label="用户姓名">
                {getFieldDecorator("name")(
                  <Input />
                )}
              </FormItem>
            </Col>
            <Col span={6}>
              <FormItem {...formItemLayout} label="身份证">
                {getFieldDecorator("identNumber")(
                  <Input />
                )}
              </FormItem>
            </Col>
            <Col span={6}>
              <Button type="primary" style={{ marginRight: 8 }} onClick={this.handleSearch}>查询</Button>
            {/*
              <Button style={{ marginLeft: 8 }} onClick={this.handleReset}>
                重置
              </Button>
            */}
              {
                type == "rFail"?<Button type="primary" loading={this.state.downloadRFailLoading} onClick={this.download}>下载列表</Button>:""
              }
              {
                type == "fail"&&failDown?<Button type="primary" loading={this.state.downloadFailLoading} onClick={this.downloadFail}>下载列表</Button>:""
              }
              {
                type == "success"&&successDown?<Button type="primary" loading={this.state.downloadSuccessLoading} onClick={this.downloadSuccess}>下载列表</Button>:""
              }
            </Col>
          </Row>
        </div>);
  }
}

module.exports = Form.create()(Filter)